<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="senior">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="systemDis" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名">
        <template scope="scope">
          <span @dblclick="getEmployees(scope.$index, scope.row)" v-html="scope.row.name"></span>
        </template>
      </el-table-column>
      <el-table-column prop="sn" label="编号">
      </el-table-column>
<!--      <el-table-column prop="systemDetail.name" label="产品类型">-->
<!--      </el-table-column>-->


      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="total" style="float:right;">
      </el-pagination>
		</el-col>

		<!--新增或者编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">

        <el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="数据字典" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--员工新增和编辑界面-->
    <el-dialog :title="title" :visible.sync="empEditFormVisible" :close-on-click-modal="false">
      <el-form :model="empEditForm" label-width="80px" :rules="empEditFormRules" ref="editForm">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="empEditForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择类型">
          <el-select v-model="empEditForm.types" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in dapt"
                :key="item.id"
                :label="item.name"
                :value="item">
              <!--
              value代表要传给后端的对象
              label代表页面显示的数据
              -->
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="empEditFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="empEditSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉表格-->
    <el-drawer
        title="我嵌套了表格!"
        :visible.sync="table"
        direction="rtl"
        size="50%">

      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.keyword" placeholder="员工查询"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="find">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>


        <!--列表-->
        <el-table :data="gridData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column type="index" width="60">
          </el-table-column>
          <el-table-column prop="name" label="产品名称" width="120" sortable>
          </el-table-column>
          <el-table-column prop="types.name" label="产品类型" width="120" sortable>
          </el-table-column>

          <!--      <el-table-column prop="shop.name" width="120" label="店铺" min-width="180" sortable>-->
          <!--      </el-table-column>-->

          <el-table-column label="操作" width="150">
            <template slot="header" slot-scope="scope">
              <el-button type="success" size="small" @click="empHandleAdd(scope.$index, scope.row)">
                新增
              </el-button>
            </template>
            <template scope="scope">
              <el-button size="small" @click="empHandleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="emphandleDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
          <el-button type="danger" @click="empBatchRemove" :disabled="this.sels.length===0">批量删除</el-button>
          <el-pagination layout="prev, pager, next" @current-change="empHandleCurrentChange" :page-size="pageSizes" :current-page="currentPages" :total="totals" style="float:right;">
          </el-pagination>
        </el-col>
    </el-drawer>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
          keyword:'',
          keywords:'',
				},
				systemDis: [],
				total: 0,
				pageSize: 5,
        currentPage:1,
        title:"",
        // 员工页码，总条数和每页显示条数
        totals: 0,
        pageSizes: 2,
        currentPages:1,
        titles:"",
        employee:[],
        deparTree:[],
        dapt:[],
        logininfo:[],

				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
        empEditFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					],
          sn: [
						{ required: true, message: '请输入部门编号', trigger: 'blur' }
					]
				},
        empEditFormRules: {
          username: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        },
        table: false,
        dialog: false,
        loading: false,
        gridData: [],

				//编辑界面数据
				editForm: {
          id:null,
          sn:"",
          name:"",
          systemDetail:null,
				},
        //员工编辑界面数据
        empEditForm: {
          id:null,
          name:null,
          types_id:null,
          types:null,
        },


			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getSystems();
			},
      // 员工根据页面查询数据
      empHandleCurrentChange(val) {
        this.currentPages = val;
        this.emphandleDel()
      },
			//获取用户列表
			getSystems() {
        let para = {
          // 模糊查询
          keyword: this.filters.keyword,
          // 当前显示条数
          pageSize: this.pageSize,
          // 当前页数
          currentPage: this.currentPage
        };
				this.listLoading = true;
				// 发起axios请求
        this.$http.post("/system",para).then(result=>{
          // 将后端获取的数据赋值给页面
          this.systemDis = result.data.rows
          console.log(result.data.total)
          // 将总条数赋值给页面
          this.total = result.data.total
          // 关闭忙等
          this.listLoading = false;
        })

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/system/"+row.id).then(result=>{
					  // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            // 调用查询方法
            this.getSystems()
            if (result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }
          }).catch(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            this.$message({
              message: result.data.message,
              type: 'error'
            });
          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = "修改",
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				// 获取row的dirPath在通过/进行分割
        let arr = row.dirPath.split("/")
        // 创建一个新的数组
        let arrays = [];
				// 这里的1是去除第一个数字，后面的减1是去除最后一个数字
        for (let i = 1;i < arr.length - 1;i++){
          // 将转为int类型的数据放进新的数组里面
          arrays.push(parseInt(arr[i]));
        }
        // 最后设置进去
        this.editForm.parent = arrays;
			},
      //员工显示编辑界面
      empHandleEdit: function (index, row) {
        this.title = "修改",
            this.empEditFormVisible = true;
        this.empEditForm = Object.assign({}, row);
        console.log(this.empEditForm)
        console.log("-----")
      },
			//显示新增界面
			handleAdd: function () {
			  this.title = "新增";
				this.editFormVisible = true;
				this.editForm = {
          id:null,
          sn:"",
          name:"",
          systemDetail:null,
				};
			},
      //显示员工新增和修改界面
      empHandleAdd: function () {
        this.title = "新增"
        this.empEditFormVisible = true;
        this.empEditForm = {
          id:null,
          name:null,
          types_id:null,
          types:null,
        };
      },
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
              // 先判断部门是否为null
              if (para.parent!=null){
                // 获取并且回显上级部门
                para.parent = {"id": para.parent[para.parent.length-1]}
              }
              // 发起请求
              this.$http.put("/system",para)
                  .then(result=>{
                    // 关闭按钮忙等
                    this.editLoading = false;
                    // 关闭模态框
                    this.editFormVisible = false;
                    // 修改页码为第一页
                    this.currentPage = 1;
                    // 调用查询方法
                    this.getSystems()
                    if (result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }
                  }).catch(result=>{
                // 关闭按钮忙等
                this.editLoading = false;
                // 修改页码为第一页
                this.currentPage = 1;
                this.$message({
                  message: "系统繁忙",
                  type: 'error'
                });
              })
						});
					}
				});
			},
      //员工新增和编辑
      empEditSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              //NProgress.start();
              // 获取的数据改为员工表
              let para = Object.assign({}, this.empEditForm);
              // 发起axios请求
              this.$http.put("/systemDet",para)
                  .then(result=>{
                    // 关闭按钮忙等
                    this.editLoading = false;
                    // 关闭模态框
                    this.empEditFormVisible = false;
                    // 修改页码为第一页
                    this.currentPage = 1;
                    // 关闭抽屉表格
                    this.table = false;
                    if (result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }
                    // 调用查询方法
                    this.getSystems()
                  }).catch(result=>{
                // 关闭忙等
                this.editLoading = false;
                // 修改页码为第一页
                this.currentPage = 1;
                this.$message({
                  message: "系统繁忙",
                  type: 'error'
                });
              })


            });
          }
        });
      },
			selsChange: function (sels) {
				this.sels = sels;
			},
			//产品批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/system",ids).then(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            // 调用查询方法
            this.getSystems()
            if (result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }
          }).catch(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            this.$message({
              message: "系统繁忙",
              type: 'error'
            });
          })
        })
				.catch(() => {

				});
			},
      //产品抽屉批量删除
      empBatchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          // 发起请求
          this.$http.patch("/systemDet",ids).then(result=>{
            // 关闭忙等
            this.listLoading = false;

            // 修改页码为第一页
            this.currentPage = 1;
            // 关闭抽屉表格
            this.table = false;
            if (result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }
            // 调用查询方法
            this.getSystems()
          }).catch(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            this.$message({
              message: "系统繁忙",
              type: 'error'
            });
          })
        })
            .catch(() => {

            });
      },

      senior(){
        this.currentPage = 1
        this.getSystems();
      },
      // 查询所有员工
      getEmployee(){
        this.$http.get("/employee")
            .then(result=>{
              this.employee = result.data
            }).catch(result=>{

        })
      },
      // 获取部门树数据
      getTree(){
			  this.$http.get("/dept/findtree")
        .then(result=>{
          this.deparTree = result.data;
        }).catch(Result=>{

        })
      },
      //获取抽屉员工列表
      getEmployees(index, row) {
        let para = {
          pageSizes: this.pageSizes,
          currentPages: this.currentPages,
          // 模糊查询
          keywords: this.filters.keywords,

        };
        this.listLoading = true;
        // 发送axios请求获取员工数据
        this.$http.get("/system/findSystemById/"+row.id,para)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              // 将员工数据赋值给页面
              this.gridData = result.data;
              // 赋值总条数
              // this.total = result.data;
              // console.log(result.data.total)
            }).catch(result=>{

        })
        this.table = true;

      },
      // //根据id删除抽屉员工
      emphandleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          // 发起删除请求
          this.$http.delete("/systemDet/"+row.id).then(result=>{
            // 关闭忙等
            this.listLoading = false;
            console.log("-------")
            console.log(row.id)
            // 将页面改为第一页
            this.currentPages = 1;
            // 刷新部门页面
            this.getSystems()
            // 关闭抽屉框
            this.table = false;

            // 判断是否为true
            if(result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'})
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'})
            }
          }).catch(result=>{
            this.$message({
              message: "系统繁忙",
              type: 'error'})
          })
        }).catch(() => {

        });
      }
    ,
    // 查询所有部门
    dapts(){
      this.$http.get("/system")
          .then(result=>{
            this.dapt = result.data
            console.log(result.data)
          }).catch(result=>{

      })
    },
    // 查询所有登录数据
    getLogininfo(){
      this.$http.get("/logininfo")
          .then(result=>{
            this.logininfo = result.data
            console.log("-----")
            console.log(result.data)
          }).catch(result=>{

      })
    },
    find(){
      this.currentPage = 1;
      this.getEmployees()
      },

		},
		mounted() {
		  // 查询所有品牌
			this.getSystems();
			// 查询新增模态框的下拉框数据
			this.getEmployee();
			this.dapts();
		}
	}

</script>

<style scoped>

</style>